package com.ysd.lis.service.iqc.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.iqc.*;
import com.ysd.lis.entity.lab.LabInstrSetup;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.lab.LabManufacturer;
import com.ysd.lis.entity.qc.QcInstrItem;
import com.ysd.lis.entity.qc.QcMaterial;
import com.ysd.lis.entity.qc.QcMaterialItem;
import com.ysd.lis.mapper.iqc.LimsIQCMaterialBatchInsAnssMapper;
import com.ysd.lis.mapper.iqc.LimsIQCMaterialBatchesMapper;
import com.ysd.lis.mapper.lab.LabInstrSetupMapper;
import com.ysd.lis.mapper.lab.LabItemDictMapper;
import com.ysd.lis.mapper.lab.LabManufacturerMapper;
import com.ysd.lis.mapper.qc.QcInstrItemMapper;
import com.ysd.lis.mapper.qc.QcMaterialItemMapper;
import com.ysd.lis.mapper.qc.QcMaterialMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.iqc.LimsIQCMaterialBatchInsAnssService;
import com.ysd.lis.service.iqc.LimsIQCMaterialBatchesService;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-01-08
 */
@Service
@Slf4j
public class LimsIQCMaterialBatchInsAnssServiceImpl extends ServiceImpl<LimsIQCMaterialBatchInsAnssMapper, LimsIQCMaterialBatchInsAnss> implements LimsIQCMaterialBatchInsAnssService {

    @Autowired
    LabInstrSetupMapper labInstrSetupMapper;
    @Autowired
    LimsIQCMaterialBatchInsAnssMapper limsIQCMaterialBatchInsAnssMapper;
    @Autowired
    QcMaterialMapper qcMaterialMapper;
    @Autowired
    LabItemDictMapper labItemDictMapper;
    @Autowired
    QcInstrItemMapper qcInstrItemMapper;
    @Autowired
    QcMaterialItemMapper qcMaterialItemMapper;
    @Override
    public Result syncQcMaterialItem(CommonPageDto pageParam) {

        List<LimsIQCMaterialBatchInsAnss> materialBatchInsAnssList = this.getGdbLisMaterialItemList();
        System.out.println(materialBatchInsAnssList);


        List<String> instrCodeList = materialBatchInsAnssList.stream().map(LimsIQCMaterialBatchInsAnss::getInstrCode).distinct().collect(Collectors.toList());
        //根据code查询仪器
        MPJLambdaWrapper<LabInstrSetup> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(LabInstrSetup::getDelFlag,0)
                .in(LabInstrSetup::getInstrCode,instrCodeList);
        List<LabInstrSetup> instrSetupList = labInstrSetupMapper.selectList(queryWrapper);

        for(LimsIQCMaterialBatchInsAnss anss : materialBatchInsAnssList){
            //找仪器
            String instrCode = anss.getInstrCode();
            List<LabInstrSetup> collect = instrSetupList.stream().filter(a -> a.getInstrCode().equals(instrCode)).collect(Collectors.toList());
            LabInstrSetup labInstrSetup = collect.get(0);

            //仪器，批号，水平 确定一条质控物
            LambdaQueryWrapper<QcMaterial> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(QcMaterial::getInstrId,labInstrSetup.getId())
                    .eq(QcMaterial::getBatchNum,anss.getBatchNumber())
                    .eq(QcMaterial::getHorizontal,anss.getHorizontal())
                    .eq(QcMaterial::getDelFlag,0);
            List<QcMaterial> qcMaterials = qcMaterialMapper.selectList(queryWrapper2);
            if(ToolsUtils.isNotEmpty(qcMaterials)){
                if(qcMaterials.size()==1){
                    //当前数据对应得质控物
                    QcMaterial qcMaterial = qcMaterials.get(0);
                    //根据项目code和仪器得报告单元查出报告项目
                    String rptGroup = labInstrSetup.getRptGroup();
                    String itemNo = anss.getItemNo();
                    LambdaQueryWrapper<LabItemDict> queryWrapper1 = new LambdaQueryWrapper<>();
                    queryWrapper1.eq(LabItemDict::getDelFlag,0)
                            .eq(LabItemDict::getRptGroup,rptGroup)
                            .eq(LabItemDict::getItemNo,itemNo);
                    List<LabItemDict> labItemDicts = labItemDictMapper.selectList(queryWrapper1);
                    if(ToolsUtils.isNotEmpty(labItemDicts)){
                        QcInstrItem qcInstrItem;

                        LabItemDict itemDict = labItemDicts.get(0);
                        //根据报告项目id，仪器ID查出仪器质控项目
                        LambdaQueryWrapper<QcInstrItem> queryWrapper3 = new LambdaQueryWrapper<>();
                        queryWrapper3.eq(QcInstrItem::getDelFlag,0)
                                .eq(QcInstrItem::getInstrId,labInstrSetup.getId())
                                .eq(QcInstrItem::getItemDictId,itemDict.getId());
                        List<QcInstrItem> qcInstrItems = qcInstrItemMapper.selectList(queryWrapper3);
                        if(ToolsUtils.isNotEmpty(qcInstrItems)){
                            qcInstrItem = qcInstrItems.get(0);
                        }else {
                            qcInstrItem = new QcInstrItem();
                            qcInstrItem.setInstrId(labInstrSetup.getId());
                            qcInstrItem.setItemDictId(itemDict.getId());
                            qcInstrItem.setSeq(itemDict.getSeq());
                            qcInstrItem.setStatus(0);
                            qcInstrItemMapper.insert(qcInstrItem);
                        }
                        //查看是否有此质控物项目
                        LambdaQueryWrapper<QcMaterialItem> queryWrapper4 = new LambdaQueryWrapper<>();
                        queryWrapper4.eq(QcMaterialItem::getDelFlag,0)
                                .eq(QcMaterialItem::getMaterialId,qcMaterial.getId())
                                .eq(QcMaterialItem::getInstrId,labInstrSetup.getId())
                                .eq(QcMaterialItem::getQcItemId,qcInstrItem.getId());
                        int count  = qcMaterialItemMapper.selectCount(queryWrapper4);
                        if(count<=0){
                            QcMaterialItem qcMaterialItem = new QcMaterialItem();
                            qcMaterialItem.setMaterialId(qcMaterial.getId());
                            qcMaterialItem.setQcItemId(qcInstrItem.getId());
                            qcMaterialItem.setInstrId(labInstrSetup.getId());
                            qcMaterialItem.setItemDictId(itemDict.getId());
                            qcMaterialItem.setEnableDate(anss.getEnableTime());
                            qcMaterialItem.setValidDate(qcMaterial.getValidDate());
                            qcMaterialItem.setReportBasis(qcMaterial.getReportBasis());
                            qcMaterialItem.setEntryDate(new Timestamp(new Date().getTime()));
                            qcMaterialItem.setEntryOperator("1");
                            qcMaterialItem.setSeq(itemDict.getSeq());
                            qcMaterialItem.setStatus(0);
                            int insert = qcMaterialItemMapper.insert(qcMaterialItem);
                            System.out.println(insert);
                        }

                    }

                }else {
                    System.out.println("多条数据");
                }
            }


        }
        return null;
    }

    private List<LimsIQCMaterialBatchInsAnss> getGdbLisMaterialItemList() {
        MPJLambdaWrapper<LimsIQCMaterialBatchInsAnss> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LimsIQCMaterialBatchInsAnss.class)
                .selectAs("ins",LimsInss::getCode,LimsIQCMaterialBatchInsAnss::getInstrCode)
                .selectAs("mb",LimsIQCMaterialBatches::getBatchNumber,LimsIQCMaterialBatchInsAnss::getBatchNumber)
                .selectAs("le",LimsIQCMedicineDecisionLevels::getCode,LimsIQCMaterialBatchInsAnss::getHorizontal)
                .selectAs("ans",LimsAnss::getCode,LimsIQCMaterialBatchInsAnss::getItemNo)
                .selectAs("ma",LimsIQCMaterialInsAnss::getEnableTime,LimsIQCMaterialBatchInsAnss::getEnableTime)
                .leftJoin(LimsIQCMaterialInsAnss.class,"ma",p->p.eq(LimsIQCMaterialInsAnss::getIQCMaterialId,LimsIQCMaterialBatchInsAnss::getIQCMaterialId).eq(LimsIQCMaterialInsAnss::getInsAnsId,LimsIQCMaterialBatchInsAnss::getInsAnsId))
                .leftJoin(LimsIQCMaterialBatches.class,"mb",p->p.eq(LimsIQCMaterialBatches::getId,LimsIQCMaterialBatchInsAnss::getIQCMaterialBatchId))
                .leftJoin(LimsIQCMaterials.class,"qm",p->p.eq(LimsIQCMaterials::getId,LimsIQCMaterialBatchInsAnss::getIQCMaterialId))
                .leftJoin(LimsInss.class,"ins",p->p.eq(LimsInss::getId,LimsIQCMaterialInsAnss::getInsId))
                .leftJoin(LimsAnss.class,"ans",p->p.eq(LimsAnss::getId,LimsIQCMaterialInsAnss::getAnsId))
                .leftJoin(LimsIQCMedicineDecisionLevels.class,"le",p->p.eq(LimsIQCMedicineDecisionLevels::getId,LimsIQCMaterials::getIQCMedicineDecisionLevelId))
                .eq(LimsIQCMaterialBatchInsAnss::getIsDeleted,0)
                .eq(LimsIQCMaterialInsAnss::getIsDeleted,0)
                .eq(LimsIQCMaterialBatches::getIsDeleted,0)
                .eq(LimsIQCMaterials::getIsDeleted,0);

        List<LimsIQCMaterialBatchInsAnss> limsIQCMaterialBatchInsAnsses = limsIQCMaterialBatchInsAnssMapper.selectList(queryWrapper);
        return limsIQCMaterialBatchInsAnsses;

    }
}
